package com.springboot.controller;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.math.BigDecimal;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Map;

import com.alibaba.fastjson.JSON;

import com.springboot.redis.RedisUtil;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;

import java.util.Collections;

import java.util.Date;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import com.springboot.service.CategoryServiceImp;

import com.springboot.entity.Category;

import com.springboot.entity.CategoryExample;

import com.springboot.entity.*;
import com.springboot.dao.*;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.multipart.MultipartFile;


@Controller

@RequestMapping(value = "/")
public class CategoryController {


    private static final Log logger = LogFactory.getLog(CategoryController.class);

    @Autowired
    private CategoryServiceImp categoryservice;

    @Resource
    private RedisUtil redisUtil;


    //定义方法addcategory,响应页面addcategory请求

    @RequestMapping(value = "addcategory")

    public String addcategory(HttpServletRequest request, HttpServletResponse response, HttpSession session) {

        //输出日志，当前执行方法为addcategory

        logger.debug("CategoryController.addcategory ......");

        categoryservice.addcategory(request, session);

        return "addcategory";

    }

    @ResponseBody
    @RequestMapping(value = "getAllCategoryactjson")
    public Map getAllCategory(HttpServletRequest request, HttpSession session){
        Map result = (Map) redisUtil.get("category");
        if (result == null) {
            result = categoryservice.searchcategoryjson("");
            redisUtil.set("category", result);
            return result;
        }
        return result;
    }


    //定义addcategoryact方法,将分类信息插入到数据库的t_category表中

    @RequestMapping(value = "addcategoryact")

    public String addcategoryact(HttpServletRequest request, HttpSession session, HttpServletResponse response, Category category) throws IOException {

        //输出当前方法日志，表示正在执行CategoryController.addcategoryact方法

        logger.debug("CategoryController.addcategoryact ......");

        categoryservice.addcategoryact(request, session, category); //返回分类管理方法，执行分类信息的查询

        return "forward:/categorymanage.action";

    }


    @ResponseBody
    //定义addcategoryactjson方法

    @RequestMapping(value = "addcategoryactjson")

    public String addcategoryactjson(HttpServletRequest request, HttpSession session, HttpServletResponse response, @RequestBody Category category) throws IOException {

        //输出当前方法日志，表示正在执行CategoryController.addcategoryactjson方法

        logger.debug("CategoryController.addcategoryactjson ......");


        //调用服务层addcategoryact方法

        categoryservice.addcategoryactjson(category);

        //返回addcategory方法

        return "success";

    }


    //定义categorymanage方法响应页面请求

    @RequestMapping(value = "categorymanage")

    public String categorymanage(HttpServletRequest request, HttpSession session, HttpServletResponse response) {

        //输出日志，表示当前正在执行CategoryController.categorymanage

        logger.debug("CategoryController.categorymanage ......");


        categoryservice.categorymanage(request, session); //返回到分类管理页面

        return "categorymanage";

    }


    // 定义 categoryview方法

    @RequestMapping(value = "categoryview")

    public String categoryview(HttpServletRequest request, HttpSession session, HttpServletResponse response) {

        // 输出日志，表示当前正在执行CategoryController.categoryview方法

        logger.debug("CategoryController.categoryview ......");


        categoryservice.categoryview(request, session);

        // 返回分类查看页面

        return "categoryview";

    }


    // 定义 updatecategory方法

    @RequestMapping(value = "updatecategory")

    public String updatecategory(HttpServletRequest request, HttpServletResponse response, HttpSession session, int id) {

        // 输出日志CategoryController.updatecategory，表示正在执行该方法

        logger.debug("CategoryController.updatecategory ......");


        categoryservice.updatecategory(request, session, id); // 返回修改分类页面

        return "updatecategory";

    }


    // 定义updatecategoryact处理分类修改

    @RequestMapping(value = "updatecategoryact")

    public String updatecategoryact(HttpServletRequest request, HttpServletResponse response, Category category, HttpSession session) throws IOException {

        // 输出日志，表示正在执行当前方法CategoryController.updatecategoryact

        logger.debug("CategoryController.updatecategoryact ......");

        categoryservice.updatecategoryact(request, category, session);

        // 返回分类管理方法

        return "forward:/categorymanage.action";

    }

    @ResponseBody
    // 定义updatecategoryactjson处理分类修改

    @RequestMapping(value = "updatecategoryactjson")

    public String updatecategoryactjson(HttpServletRequest request, HttpServletResponse response, @RequestBody Category category, HttpSession session) throws IOException {

        // 输出日志，表示正在执行当前方法CategoryController.updatecategoryactjson

        logger.debug("CategoryController.updatecategoryactjson ......");

        categoryservice.updatecategoryactjson(category);

        return "success";

    }


    // 定义deletecategory,处理删除分类

    @RequestMapping(value = "deletecategory")

    public String deletecategory(HttpServletRequest request, HttpServletResponse response, HttpSession session, int id) {

        // 输出日志，表示当前正在执行CategoryController.deletecategory方法

        logger.debug("CategoryController.deletecategory ......");

        categoryservice.deletecategory(request, session, id);

        // 返回分类管理方法

        return "forward:/categorymanage.action";

    }


    @ResponseBody
    // 定义deletecategoryjson,处理删除分类

    @RequestMapping(value = "deletecategoryjson")

    public String deletecategoryjson(HttpServletRequest request, HttpServletResponse response, HttpSession session, int id) {

        // 输出日志，表示当前正在执行CategoryController.deletecategory方法

        logger.debug("CategoryController.deletecategory ......");

        categoryservice.deletecategory(request, session, id);

        return "success";

    }


    // 定义searchcategory方法，处理搜索操作

    @RequestMapping(value = "searchcategory")

    public String searchcategory(HttpServletRequest request, HttpServletResponse response, HttpSession session, String search) {

        // 输出日志，表示当前正在执行CategoryController.searchcategory

        logger.debug("CategoryController.searchcategory ......");


        categoryservice.searchcategory(request, session, search); // 返回查询分类页面

        return "searchcategory";

    }


    @ResponseBody
    // 定义searchcategoryjson方法，处理搜索操作
    @RequestMapping(value = "searchcategoryjson")

    public Map searchcategoryjson(HttpServletRequest request, HttpServletResponse response, HttpSession session, String search) {

        // 输出日志，表示当前正在执行CategoryController.searchcategoryjson

        logger.debug("CategoryController.searchcategoryjson ......");


        // 定义返回结果

        Map result = new HashMap();

        // 获取返回结果

        result = categoryservice.searchcategoryjson(search); // 将结果以json返回

        return result;

    }


    // 定义CategorypinglunMapper

    @RequestMapping(value = "categorydetails")

    public String categorydetails(HttpServletRequest request, HttpSession session, HttpServletResponse response, int id) {

        // 输入日志信息，表名当前执行方法为CategoryController.categorydetails

        logger.debug("CategoryController.categorydetails ......");


        categoryservice.categorydetails(request, session, id); // 返回分类详情页面

        return "categorydetails";

    }


    @ResponseBody
    // 定义Category详情方法

    @RequestMapping(value = "categorydetailsjson")

    public Map categorydetailsjson(HttpServletRequest request, HttpSession session, HttpServletResponse response, int id) {

        // 输入日志信息，表名当前执行方法为CategoryController.categorydetailsjson

        logger.debug("CategoryController.categorydetailsjson ......");


        Map result = new HashMap();

        result = categoryservice.categorydetailsjson(id);

        // 将结果以json返回

        return result;

    }


}

